package com.struts2.example.action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.struts2.example.model.Connect;
import com.struts2.example.model.Question;


public class ListQuestionsAction extends ActionSupport {
	private String TAG="ListQuestionAction";
	private static final long serialVersionUID = 1L;
	private Question questionBean;
	private List<Question> listQuestions;

	public String execute() {
		System.out.print(TAG+"Question()"+"\n");
		
		listQuestions=new ArrayList<Question>();
		String ret = ERROR;
		Connect conn = new Connect();
		Connection connected = conn.Connected();
		
		try {
			String sql = "SELECT * FROM testonline_bang_dai.question";
			PreparedStatement ps = connected.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				questionBean=new Question();
				questionBean.setQuestionId(rs.getInt(1));
				questionBean.setQuestionText(rs.getString(2));
				listQuestions.add(questionBean);
				ret = SUCCESS;
			}
		} catch (Exception e) {
			ret = ERROR;
		} finally {
			if (connected != null) {
				try {
					connected.close();
				} catch (Exception e) {
				}
			}
		}
		return ret;
	}

	public Question getQuestionBean() {
		System.out.print(TAG+"getQuestion()"+"\n");
		return this.questionBean;
	}

	public void setQuestionBean(Question quesionBean) {
		System.out.print(TAG+"setQuestion()"+"\n");
		this.questionBean = quesionBean;
	}
	
	public List<Question> getListQuestions()
	{
		return listQuestions;
	}
	
	public void setListQuestion(List<Question> listQuestions)
	{
		this.listQuestions=listQuestions;
	}
}
